package org.jetnuke.blocks;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.jforum.SessionFacade;
import net.jforum.dao.UserDAO;
import net.jforum.entities.User;
import net.jforum.entities.UserSession;
import net.jforum.util.preferences.ConfigKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IBlock;
import org.jetnuke.common.IBlockInfo;
import org.jetnuke.common.IUserInfo;

public class UserInfo implements IBlock {

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.common.IBlock#serviceBlock(javax.servlet.http.HttpServletRequest,
	 *      org.apache.velocity.VelocityContext, org.jetnuke.common.IBlockInfo)
	 */
	public void serviceBlock(HttpServletRequest req, VelocityContext vc,
			IBlockInfo blockInfo, IUserInfo userInfo, IAdminInfo adminInfo)
			throws Exception {
		if (userInfo != null) {
			/*Criteria crt = new Criteria();
			crt.add(privmsgsPeer.TO_USERID, userInfo.getID());
			Criteria.Criterion a1 = crt.getNewCriterion(privmsgsPeer.TYPE,
					new Integer(IConstants.PRIVMSGS_NEW_MAIL), Criteria.EQUAL);
			Criteria.Criterion a5 = crt.getNewCriterion(privmsgsPeer.TYPE,
					new Integer(IConstants.PRIVMSGS_UNREAD_MAIL),
					Criteria.EQUAL);
			crt.add((a5.or(a1)));
			vc.put("newmsg", new Integer(privmsgsPeer.count(crt)));
			vc.put("oldmsg", new Integer(privmsgsPeer.count(new Criteria().add(
					privmsgsPeer.TO_USERID, userInfo.getID()).add(
							privmsgsPeer.TYPE,
					new Integer(IConstants.PRIVMSGS_READ_MAIL)))));*/
			//PrivateMessageModel pmm=net.jforum.model.DataAccessDriver.getInstance().newPrivateMessageModel();
			//User user = net.jforum.model.DataAccessDriver.getInstance().newUserModel().selectById(userInfo.getID());
			vc.put("newmsg",new Integer(userInfo.getPrivateMessagesCount()));
			//pmm.
			vc.put("name", userInfo.getUname());
		}
		
		UserDAO um=net.jforum.dao.DataAccessDriver.getInstance().newUserDAO();
		User us_=um.getLastUserInfo();
		
		//List ls = usersPeer.doSelect(new Criteria().addDescendingOrderByColumn(
			//	usersPeer.UID).setLimit(1));
		if (us_!=null) {
			vc.put("lastuser", us_);
		}
		vc.put("count", new Integer(um.getTotalUsers()));
		
		
		vc.put("online", new Integer(SessionFacade.size()));
		int guest = 0;
		int registered = 0;
		int aid = SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID);
	
		List userSessions = SessionFacade.getAllSessions();
		List onlineUsersList = new ArrayList();
		for (Iterator iter = userSessions.iterator(); iter.hasNext(); ) {
			UserSession us = (UserSession)iter.next();
			
			if (us.getUserId() == aid) {
				guest++;
			}
			else {
				registered++;
				onlineUsersList.add(us);
			}
		}
		vc.put("userSessions", onlineUsersList);		
		vc.put("members", new Integer(registered));
		vc.put("guests", new Integer(guest));
	}

}
